Page({
    data: {
      commentList: []
    },
  
    onLoad() {
      this.getMyComments();
    },
  
    getMyComments() {
      const userInfo = wx.getStorageSync('userInfo');
      if (!userInfo || !userInfo.userId) {
        wx.showToast({ title: '用户未登录', icon: 'none' });
        return;
      }
  
      wx.request({
        url: 'http://localhost:8080/comment/listByUserId?userId=' + userInfo.userId,
        method: 'POST',
        data: {
          page: 1,
          limit: 50,
        //   userId: userInfo.userId
        },
        success: (res) => {
          if (res.data.code === 0) {
            const list = res.data.data.map(item => ({
              reserveId: item.reserveId,
              branchName: item.branchName,
              comment: item.comment,
              replay: item.replay,
              replayTime: item.replayTime,
              reserveTimeFormat: this.formatDate(item.reserveTime),
              expand: false,
              showToggle: item.comment.length >= 50 ,
              expand1: false,
              showToggle1: item.comment.length >= 50 
            }));
            this.setData({ commentList: list });
          } else {
            wx.showToast({ title: res.data.msg || '获取失败', icon: 'none' });
          }
        }
      });
    },
  
    toggleExpand(e) {
      const index = e.currentTarget.dataset.index;
      const list = this.data.commentList;
      list[index].expand = !list[index].expand;
      this.setData({ commentList: list });
    },
  
    formatDate(dateStr) {
      if (!dateStr) return '';
      const date = new Date(dateStr);
      const y = date.getFullYear();
      const m = ('0' + (date.getMonth() + 1)).slice(-2);
      const d = ('0' + date.getDate()).slice(-2);
      return `${y}-${m}-${d}`;
    }
  });
  